package com.example.administrator.easy.detailxinfo;

import android.drm.DrmStore;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.DetailsAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class DetailsToDiscover extends AppCompatActivity {
    private ListView mListView;
    private DetailsAdapter adapter;

    //头布局
    private LinearLayout mHeadView;
    private TextView imageView;
    private ImageOptions mOptions;
    private ImageView userIv;
    private TextView tvUser,tvTime,tvView,tvTuijian;

    private List<DetailBean> list;



    String tempUrl="http://m.ieasy.tv/api/article/article/49078";
    String tempUrl2="http://m.ieasy.tv/api/articlecomment/list/49078/0/0/20/0";
    private LinearLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_details_to_discover);

        initvie();
        initData();
        initheadview();
        setData();
    }

    private void setData() {
        mListView.setAdapter(adapter);

    }

    private void initheadview() {

        mHeadView= (LinearLayout) getLayoutInflater().inflate(R.layout.detail_discover_headview,null);
        mHeadView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        mLayout = (LinearLayout) mHeadView.findViewById(R.id.detail_discover_viewgroup);

        initHV(mHeadView);
        initHdata();

        mListView.addHeaderView(mHeadView);
        mOptions=new ImageOptions.Builder()
                .setCircular(true)
                .build();
        userIv= (ImageView) mHeadView.findViewById(R.id.detail_iv_head_first);
        tvUser= (TextView) mHeadView.findViewById(R.id.detail_tv_head_username);
        tvTime= (TextView) mHeadView.findViewById(R.id.detail_tv_head_time);
        tvView= (TextView) mHeadView.findViewById(R.id.detail_tv_head_muns);
        tvTuijian= (TextView) mHeadView.findViewById(R.id.detail_tv_head_tuijianren);


    }

    private void initHdata() {


        RequestParams params=new RequestParams(tempUrl);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject object=new JSONObject(result);
                    JSONObject object1=object.optJSONObject("result");

                    String userAvatar = object1.optString("UserAvatar");
                    x.image().bind(userIv,userAvatar,mOptions);
                    String content = object1.optString("Content");
                    JSONArray jsonArray = object1.optJSONArray("ArticlePictures");

                    String articleType2 = object1.optString("ArticleType");

                    if (articleType2.equals("0")){

                        TextView textView=new TextView(DetailsToDiscover.this);
                        textView.setText(content);
                        mLayout.addView(textView);
                        JSONArray jsonArray1 = object1.optJSONArray("ArticlePictures");
                        for (int i = 0; i < jsonArray1.length(); i++) {
                            JSONObject object2 = jsonArray1.optJSONObject(i);
                            String picPath1 = object2.optString("PicPath");
                            ImageView imageViewt=new ImageView(DetailsToDiscover.this);
                            x.image().bind(imageViewt,picPath1);
                            mLayout.addView(imageViewt);

                        }



                    }else {
                        String str[]=content.split("\"");


                        for (int i = 0; i < str.length; i++) {
                            boolean http = str[i].startsWith("http");
                            boolean b = str[i].startsWith(" />");
                            boolean b1 = str[i].startsWith("<");
                            if (http){
                                ImageView imageViews=new ImageView(DetailsToDiscover.this);
                                imageViews.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,250));
                                x.image().bind(imageViews,str[i]);
                                mLayout.addView(imageViews);
                            }else if (b){
                                String substring = str[i].substring(3, str[i].length() - 1);
                                String[] split = substring.split("<");
                                TextView textView1=new TextView(DetailsToDiscover.this);
                                textView1.setTextColor(Color.BLACK);
                                textView1.setText(split[0]);
                                mLayout.addView(textView1);
                            }else if (b1){

                            }else {
                                if (!str[i].startsWith(" alt") && !str[i].startsWith("<br")){
                                    TextView textView1=new TextView(DetailsToDiscover.this);
                                    String[] splits = str[i].split("<");
                                    textView1.setTextColor(Color.BLACK);
                                    textView1.setText(splits[0]);
                                    mLayout.addView(textView1);
                                }

                            }
                        }
                    }

//                    String createDate = object1.optString("CreateDate");
//                    String substring1 = createDate.substring(6, createDate.length() - 2);

//                    String[] split1 = createDate.split("\\(");
//                    String[] split2 = split1[1].split("\\)");
//                    long times=Long.getLong(substring1);
//                    long h= times%(24*60*60);
//
//                    tvTime.setText(h+"年");

                    String userNickName = object1.optString("UserNickName");
                    tvUser.setText(userNickName);
                    int viewCount = object1.optInt("ViewCount");
                    tvView.setText(viewCount+"");

                    int commentCount = object1.optInt("UpCount");
                    int commentCount2 = object1.optInt("UpCount2");
                    int commentCount3 = object1.optInt("UpCount3");
                    int commentCount4 = object1.optInt("UpCount4");
                    tvTuijian.setText(commentCount+commentCount2+commentCount3+commentCount4+"个朋友推荐 ");



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(DetailsToDiscover.this, "OnError", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initHV(View mView) {
        imageView= (TextView) mView.findViewById(R.id.detail_mengmengda);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DetailsToDiscover.this, "111", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initData() {
        RequestParams params=new RequestParams(tempUrl2);
        adapter=new DetailsAdapter(this,list);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject object=new JSONObject(result);
                    JSONArray jsonArray = object.optJSONArray("result");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        DetailBean bean=new DetailBean();
                        JSONObject object1 = jsonArray.optJSONObject(i);
                        String content = object1.optString("Content");
                        bean.count=content;
                        String userNickName = object1.optString("UserNickName");
                        bean.userName=userNickName;
                        String userAvatar = object1.optString("UserAvatar");
                        bean.urlUser=userAvatar;
                        int userId = object1.optInt("UserId");
                        bean.userId=userId;
                        String commentIco = object1.optString("CommentIco");
                        bean.urlCount=commentIco;
                        list.add(bean);
                    }
                    adapter.notifyDataSetChanged();



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initvie() {
        mListView= (ListView) findViewById(R.id.detail_main_lv_discover);
        list=new ArrayList<>();
    }

    public void click(View view) {
        switch (view.getId()){

            case R.id.detail_discover_lv_back:
                finish();
                break;
        }
    }
}
